package com.erp.action;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.json.JSONArray;
import org.json.JSONObject;

import com.erp.dto.Page;
import com.erp.service.ApplyService;
import com.erp.service.DepartmentService;
import com.erp.service.ExamineService;
import com.erp.service.ForumService;
import com.erp.service.PrivilegeService;
import com.erp.service.ProcessDeploymentService;
import com.erp.service.ReplyService;
import com.erp.service.RoleService;
import com.erp.service.TimeOffService;
import com.erp.service.TopicService;
import com.erp.service.UserService;
import com.erp.service.WorkflowService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class BaseAction extends ActionSupport implements SessionAware,
		RequestAware {

	private static final long serialVersionUID = 1L;

	protected Page page;

	public Page getPage() {
		if (page == null) {
			page = new Page();
		}
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	@Resource(name = "roleService")
	protected RoleService roleService;
	@Resource(name = "departmentService")
	protected DepartmentService departmentService;
	@Resource(name = "userService")
	protected UserService userService;
	@Resource(name = "privilegeService")
	protected PrivilegeService privilegeService;
	@Resource(name = "forumService")
	protected ForumService forumService;
	@Resource(name = "topicService")
	protected TopicService topicService;
	@Resource(name = "replyService")
	protected ReplyService replyService;
	@Resource(name = "workflowService")
	protected WorkflowService workflowService;
	@Resource(name = "procDepService")
	protected ProcessDeploymentService procDepService;
	@Resource(name = "applyService")
	protected ApplyService applyService;
	@Resource(name = "timeOffService")
	protected TimeOffService timeOffService;
	@Resource(name = "examineService")
	protected ExamineService examineService;

	protected Map<String, Object> session;
	protected Map<String, Object> request;

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	@Override
	public void setRequest(Map<String, Object> request) {
		this.request = request;
	}

	public void writeJson(String key, Object value) {
		try {

			JSONObject json = new JSONObject();
			json.put(key, value);

			getRealResp().setContentType("text/html;charset=utf-8");
			getRealResp().getWriter().write(json.toString());
			getRealResp().getWriter().flush();
			getRealResp().getWriter().close();

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void writeJson(Map<String, Object> jsonMap) {
		try {
			JSONObject json = new JSONObject(jsonMap);

			getRealResp().setContentType("text/html;charset=utf-8");
			getRealResp().getWriter().write(json.toString());
			getRealResp().getWriter().flush();
			getRealResp().getWriter().close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void writeJson(List<Map<String, Object>> infos) {
		try {
			JSONArray json = new JSONArray(infos);

			getRealResp().setContentType("text/html;charset=utf-8");
			getRealResp().getWriter().write(json.toString());
			getRealResp().getWriter().flush();
			getRealResp().getWriter().close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public HttpServletRequest getRealReq() {
		return ServletActionContext.getRequest();
	}

	public HttpServletResponse getRealResp() {
		return ServletActionContext.getResponse();
	}

	/**
	 * 将对象压入值栈，以回显数据
	 * 
	 * @param obj
	 */
	public void pushIntoValueStack(Object obj) {
		ActionContext.getContext().getValueStack().push(obj);
	}

	/*
	 * public String getOldUrl(String defaultStr){ String oldUrl =
	 * (String)session.get("oldUrl");
	 * if(oldUrl==null||"".equals(oldUrl.trim())){ return defaultStr; } return
	 * oldUrl; }
	 */
}
